package com.aiyige.page.interest.model;

import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

public class Interest implements Parcelable {

    public static final int TYPE_ADD = 0;
    public static final int TYPE_NORMAL = 1;
    public static final int TYPE_ALL = 2;

    public static final int CATEGORY_INTEREST = 1;
    public static final int CATEGORY_INDUSTRY = 2;
    public static final int CATEGORY_RECOMMEND = 3;
    public static final int CATEGORY_HOT = 4;
    public static final int CATEGORY_FOLLOW = 5;

    String id;
    String title;
    String sourceId;
    /**
     * 操作类型
     */
    int type;
    /**
     * 数据类别
     */
    int category;
    boolean selected;
    boolean edit;
    boolean recommend;

    public Interest() {
    }

    public Interest(Interest other) {
        this.id = other.id;
        this.title = other.title;
        this.type = other.type;
        this.category = other.category;
        this.selected = other.selected;
        this.edit = other.edit;
        this.recommend = other.recommend;
        this.sourceId = other.sourceId;
    }

    private Interest(Builder builder) {
        setId(builder.id);
        setTitle(builder.title);
        setSourceId(builder.sourceId);
        setType(builder.type);
        setCategory(builder.category);
        setSelected(builder.selected);
        setEdit(builder.edit);
        setRecommend(builder.recommend);
    }

    public static Builder newBuilder() {
        return new Builder();
    }


    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }


    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public int getCategory() {
        return category;
    }

    public void setCategory(int category) {
        this.category = category;
    }

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }

    public boolean isEdit() {
        return edit;
    }

    public void setEdit(boolean edit) {
        this.edit = edit;
    }

    public boolean isRecommend() {
        return recommend;
    }

    public void setRecommend(boolean recommend) {
        this.recommend = recommend;
    }

    public String getSourceId() {
        return sourceId;
    }

    public void setSourceId(String sourceId) {
        this.sourceId = sourceId;
    }

    @Override
    public int hashCode() {
        String val = (sourceId == null ? "" : sourceId) + (id == null ? "" : id) + (title == null ? "" : title) + category + type;
        return val.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (!(obj instanceof Interest)) {
            return false;
        }

        Interest other = (Interest) obj;
        if (this.type != other.type) {
            return false;
        }

        if (this.category != other.category) {
            return false;
        }

        if (!TextUtils.isEmpty(this.sourceId) && !TextUtils.isEmpty(other.sourceId)) {
            return this.sourceId.equals(other.sourceId);
        }

        boolean sameId = false;
        if (this.id == null && other.id == null) {
            sameId = true;
        } else if (this.id == null || other.id == null) {
            sameId = false;
        } else {
            sameId = this.id.equals(other.id);
        }
        if (!sameId) return false;
        boolean sameTitle = false;
        if (this.title == null && other.title == null) {
            sameTitle = true;
        } else if (this.title == null || other.title == null) {
            sameTitle = false;
        } else {
            sameTitle = this.title.equals(other.title);
        }
        if (!sameTitle) return false;
        return true;
    }


    public static final class Builder {
        private String id;
        private String title;
        private String sourceId;
        private int type;
        private int category;
        private boolean selected;
        private boolean edit;
        private boolean recommend;

        private Builder() {
            id = "";
            sourceId = "";
            title = "";
            type = TYPE_NORMAL;
            category = CATEGORY_INTEREST;
            selected = false;
            edit = false;
            recommend = false;
        }

        public Builder id(String val) {
            id = val;
            return this;
        }

        public Builder title(String val) {
            title = val;
            return this;
        }

        public Builder sourceId(String val) {
            sourceId = val;
            return this;
        }

        public Builder type(int val) {
            type = val;
            return this;
        }

        public Builder category(int val) {
            category = val;
            return this;
        }

        public Builder selected(boolean val) {
            selected = val;
            return this;
        }

        public Builder edit(boolean val) {
            edit = val;
            return this;
        }

        public Builder recommend(boolean val) {
            recommend = val;
            return this;
        }

        public Interest build() {
            return new Interest(this);
        }
    }


    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.id);
        dest.writeString(this.title);
        dest.writeString(this.sourceId);
        dest.writeInt(this.type);
        dest.writeInt(this.category);
        dest.writeByte(this.selected ? (byte) 1 : (byte) 0);
        dest.writeByte(this.edit ? (byte) 1 : (byte) 0);
        dest.writeByte(this.recommend ? (byte) 1 : (byte) 0);
    }

    protected Interest(Parcel in) {
        this.id = in.readString();
        this.title = in.readString();
        this.sourceId = in.readString();
        this.type = in.readInt();
        this.category = in.readInt();
        this.selected = in.readByte() != 0;
        this.edit = in.readByte() != 0;
        this.recommend = in.readByte() != 0;
    }

    public static final Creator<Interest> CREATOR = new Creator<Interest>() {
        @Override
        public Interest createFromParcel(Parcel source) {
            return new Interest(source);
        }

        @Override
        public Interest[] newArray(int size) {
            return new Interest[size];
        }
    };
}
